// pages/product/allEvaluate/index.js
const evaluateService = require('../../../service/evaluate')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageNum: 1, // 当前请求的page
    pageSize: 10, // 请求数据的size
    noData: false,
    nomoredata:false,
    commentList: [],
    images: [
      "https://img.yzcdn.cn/vant/cat.jpeg",
      "https://img.yzcdn.cn/vant/cat.jpeg",
      "https://img.yzcdn.cn/vant/cat.jpeg"
    ],
    spuId:"",
    
  },
  getList(callback) {
    evaluateService.getAllevaluate({
      "data": {
        //用户id
        "spuId": this.data.spuId
      },
      //分页
      "queryRequest": {
        //页码
        "pageNum": this.data.pageNum,
        //面数据量
        "pageSize": this.data.pageSize
      }

    }, res => {
      console.log(res)
     callback(res)
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      spuId:options.spuId
    })
    wx.showLoading({
      title: "加载中...",
    });
    this.getList(res => {
      //console.log(res)
      wx.hideLoading()
      if (res.code == "00000") {
        if (res.data.rows.length==0) {
          this.setData({
            commentList: res.data.rows,
            noData:true,
            nomoredata:false
          })
        } else {
          for (let i = 0; i < res.data.rows.length; i++) {
            const element = res.data.rows[i];
            element.overall = ((element.score * 0.7 * 100 + element.pdScore * 0.3 * 100) / 100).toFixed(1)
          }
          this.setData({
            commentList: res.data.rows,
            noData:false,
            nomoredata:false
          })
        }      
        
      }

    })
  },
  previewImg(e){
    //console.log(e);
    var list=[];
    e.currentTarget.dataset.imglist.forEach(element => {
      list.push(element.picUrl)
    });
    var imgUrls = e.currentTarget.dataset.item;
    //图片预览
    wx.previewImage({
      current: imgUrls, // 当前显示图片的http链接
      urls: list
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      pageNum: 1,
      noData: false,
    });
    const self = this;

    this.getList(function (res) {
      console.log(res);
      if (res.code == "00000") {
        if (res.data.rows.length==0) {
          self.setData({
            commentList: res.data.rows,
            noData:true,
            nomoredata:false
          })
        } else {
          for (let i = 0; i < res.data.rows.length; i++) {
            const element = res.data.rows[i];
            element.overall = ((element.score * 0.7 * 100 + element.pdScore * 0.3 * 100) / 100).toFixed(1)
          }
          self.setData({
            commentList: res.data.rows,
            noData:false,
            nomoredata:false
          })
        }      
        
      }
      
      wx.hideNavigationBarLoading()
      wx.stopPullDownRefresh();
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.noData) return;
    wx.stopPullDownRefresh();
    const prolist = this.data.commentList;
    let page = this.data.pageNum;
    this.setData({
      // 每次下拉 page+1
      pageNum: ++page,
    });
    const self = this;

    this.getList(function (res) {
      if (res.data.rows.length == 0) {
        self.setData({
          noData: false,
          nomoredata:true
        });
      } else {
        res.data.rows.forEach((item) => {
          item.overall = ((item.score * 0.7 * 100 + item.pdScore * 0.3 * 100) / 100).toFixed(1)
          prolist.push(item);
        });
        self.setData({
          commentList: prolist,
        });
      }
      // 隐藏加载状态
      // wx.hideNavigationBarLoading()
    });
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})